### 用户及角色管理
from datetime import datetime
from typing import List, Optional

from pydantic import BaseModel, EmailStr, Field

from datetime import datetime
from typing import Optional

from pydantic import BaseModel, Field


class BaseRole(BaseModel):
    id: int
    name: str
    desc: str = ""
    users: Optional[list] = []
    menus: Optional[list] = []
    apis: Optional[list] = []
    created_at: Optional[datetime]
    updated_at: Optional[datetime]


class RoleCreate(BaseModel):
    name: str = Field(example="管理员")
    desc: str = Field("", example="管理员角色")


class RoleUpdate(BaseModel):
    id: int = Field(example=1)
    name: str = Field(example="管理员")
    desc: str = Field("", example="管理员角色")


class RoleUpdateMenusApis(BaseModel):
    id: int
    menu_ids: list[int] = []
    api_infos: list[dict] = []




class BaseUser(BaseModel):
    id: int
    email: Optional[EmailStr] = None
    username: Optional[str] = None
    nickname: str
    is_active: Optional[bool] = True
    is_superuser: Optional[bool] = False
    created_at: Optional[datetime]
    updated_at: Optional[datetime]
    last_login: Optional[datetime]
    roles: Optional[list] = []


class UserCreate(BaseModel):
    email: EmailStr = Field(example="admin@qq.com")
    username: str = Field(example="admin")
    password: str = Field(example="123456")
    nickname: str
    is_active: Optional[bool] = True
    is_superuser: Optional[bool] = False
    role_data: Optional[List[dict]] = []
    family_id: Optional[int] = Field(0, description="家庭ID")

    def create_dict(self):
        return self.model_dump(exclude_unset=True, exclude={"role_data"})


class UserUpdate(BaseModel):
    id: int
    email: EmailStr
    username: str
    nickname: str
    is_active: Optional[bool] = True
    is_superuser: Optional[bool] = False
    role_data: Optional[List[dict]] = []
    family_id: Optional[int] = 0


class UpdatePassword(BaseModel):
    old_password: str = Field(description="旧密码")
    new_password: str = Field(description="新密码")
